package com.huanuo.internetofthings.controller.fastPeriphery;

import com.huanuo.internetofthings.bo.fastPeriphery.FastPeriphery;
import com.huanuo.internetofthings.bo.user.UserInfo;
import com.huanuo.internetofthings.dto.fastPeriphery.FastPeripheryRequestDto;
import com.huanuo.internetofthings.service.fastPeriphery.FastPeripheryFacadeService;
import com.huanuo.internetofthings.service.user.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.unicorn.framework.core.ResponseDto;
import org.unicorn.framework.core.SysCode;
import org.unicorn.framework.core.exception.PendingException;

import java.util.List;

/**
 * <p>
 * fast周边网络情况 前端控制器
 * </p>
 *
 * @author xiebin
 * @since 2018-07-25
 */
@RestController
@Api(value = "FAST周边网络情况", tags = { "FAST周边网络情况" })
@RequestMapping("/fastPeriphery")
public class FastPeripheryController {
	
	@Autowired
	private FastPeripheryFacadeService fastPeripheryFacadeService;
	
	@Autowired
	private UserInfoService userInfoService;

	@ApiOperation("Fast周边网络")
	@RequestMapping(value = { "/info" }, method = { RequestMethod.POST })
	public ResponseDto<List<FastPeriphery>> getFastPeripheryList(@RequestBody FastPeripheryRequestDto fastPeripheryRequestDto) throws PendingException {
		// 获得用户信息
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		return fastPeripheryFacadeService.getFastPeripheryList(fastPeripheryRequestDto);
	}

	/**
	 * 检查用户
	 * 
	 * @param userIdentity
	 * @return
	 * @throws PendingException
	 */
	public UserInfo checkUser(String userIdentity) throws PendingException {
		// 根据用户标识查询用户信息
		UserInfo userInfo = userInfoService.getUserInfoByCondition(UserInfo.builder().userIdentity(userIdentity).build());
		if (userInfo == null) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		if (StringUtils.isBlank(userInfo.getGroupNo())) {
			throw new PendingException(SysCode.SYS_FAIL, "用户不属于任何组");
		}
		return userInfo;
	}
}
